package logic

import (
	"context"
	"strings"
	"time"

	"mall/common/enumx"
	"mall/common/modelx"
	"mall/service/forum/model"
	"mall/service/forum/rpc/internal/svc"
	"mall/service/forum/rpc/types/forum"

	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveCollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRemoveCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveCollectLogic {
	return &RemoveCollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RemoveCollectLogic) RemoveCollect(in *forum.RemoveCollectReq) (*forum.RemoveCollectResp, error) {
	if len(in.Ids) == 0 {
		return &forum.RemoveCollectResp{}, nil
	}

	filterMap := make(map[string]map[string][]interface{})
	filterMap[model.ForumUserCollectColumns.ObjectID] = map[string][]interface{}{
		"in": modelx.UniqueValues(strings.Split(in.Ids, ","), 64),
	}
	filterMap[model.ForumUserCollectColumns.ObjectType] = map[string][]interface{}{
		"equalTo": []interface{}{in.Type},
	}
	filterMap[model.ForumUserCollectColumns.UserID] = map[string][]interface{}{
		"equalTo": []interface{}{in.UpdatedBy},
	}
	filterMap[model.ForumUserCollectColumns.PlatformID] = map[string][]interface{}{
		"equalTo": []interface{}{in.PlatformId},
	}
	filterMap[model.ForumUserCollectColumns.Status] = map[string][]interface{}{
		"equalTo": []interface{}{enumx.ObjectStatusAproved},
	}
	_, err := l.svcCtx.ForumUser.UpdateCollectBatch(l.ctx, model.M{
		model.ForumUserCollectColumns.Status:    uint16(enumx.ObjectStatusDeleted),
		model.ForumUserCollectColumns.UpdatedAt: time.Now(),
	}, filterMap)
	if err != nil {
		return nil, err
	}

	return &forum.RemoveCollectResp{}, nil
}
